package com.tkjt.pms.calc.formula.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tkjt.pms.calc.formula.common.EngineResult;
import com.tkjt.pms.calc.formula.common.FormulaConsts;
import com.tkjt.pms.calc.formula.common.FormulaRule;
import com.tkjt.pms.calc.formula.common.FormulaValue;
import com.tkjt.pms.common.IndexConsts;
import com.tkjt.pms.common.IndexParam;

public class FormulaParamHelper {
	
	public static List<String> getCalcWhichDatas(IndexParam indexParam) {
		@SuppressWarnings("unchecked")
		List<String> whichDatas = (List<String>)indexParam.getParam(FormulaConsts.ITEM_CONFIG_WHICH_DATAS);
		return whichDatas;
	}
	
	public static IndexParam setFormulaCurrentWhichData(IndexParam indexParam, String whichData) {
		indexParam.addParam(FormulaConsts.ITEM_FORMULA_WHICH_DATA, whichData);
		return indexParam;
	}
	
	public static String getFormulaCurrentWhichData(IndexParam indexParam) {
		String whichData = (String)indexParam.getParam(FormulaConsts.ITEM_FORMULA_WHICH_DATA);
		return whichData;
	}
	
	public static String getFormulaIndexYearMonth(IndexParam indexParam) {
		String indexYearMonth = (String)indexParam.getParam(IndexConsts.PARAM_INDEX_YEAR_MONTH);
		return indexYearMonth;
	}
	
	@SuppressWarnings("unchecked")
	public static IndexParam addFormulaRule(IndexParam indexParam, String whichData, FormulaRule value) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			whichDataFormulaInfos = new HashMap<String, Object>();
			indexParam.addParam(whichData, whichDataFormulaInfos);
		}
		whichDataFormulaInfos.put(FormulaConsts.ITEM_FORMULA_RULE, value);
		return indexParam;
	}
	
	@SuppressWarnings("unchecked")
	public static FormulaRule getFormulaRule(IndexParam indexParam, String whichData) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			return null;
		}
		FormulaRule formulaRule = (FormulaRule)whichDataFormulaInfos.get(FormulaConsts.ITEM_FORMULA_RULE);
		return formulaRule;
	}
	
	@SuppressWarnings("unchecked")
	public static IndexParam addFormulaValue(IndexParam indexParam, String whichData, FormulaValue value) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			whichDataFormulaInfos = new HashMap<String, Object>();
			indexParam.addParam(whichData, whichDataFormulaInfos);
		}
		whichDataFormulaInfos.put(FormulaConsts.ITEM_FORMULA_VALUE, value);
		return indexParam;
	}
	
	@SuppressWarnings("unchecked")
	public static FormulaValue getFormulaValue(IndexParam indexParam, String whichData) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			return null;
		}
		FormulaValue formulaValue = (FormulaValue)whichDataFormulaInfos.get(FormulaConsts.ITEM_FORMULA_VALUE);
		return formulaValue;
	}
	
	@SuppressWarnings("unchecked")
	public static IndexParam addFormulaException(IndexParam indexParam, String whichData, String exception) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			whichDataFormulaInfos = new HashMap<String, Object>();
			indexParam.addParam(whichData, whichDataFormulaInfos);
		}
		whichDataFormulaInfos.put(FormulaConsts.ITEM_FORMULA_EXCEPTION, exception);
		return indexParam;
	}
	
	@SuppressWarnings("unchecked")
	public static String getFormulaException(IndexParam indexParam, String whichData) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			return null;
		}
		String exception = (String)whichDataFormulaInfos.get(FormulaConsts.ITEM_FORMULA_EXCEPTION);
		return exception;
	}
	
	@SuppressWarnings("unchecked")
	public static IndexParam addFormulaResult(IndexParam indexParam, String whichData, EngineResult value) {
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			whichDataFormulaInfos = new HashMap<String, Object>();
			indexParam.addParam(whichData, whichDataFormulaInfos);
		}
		whichDataFormulaInfos.put(FormulaConsts.ITEM_FORMULA_RESULT, value);
		return indexParam;
	}
	
	public static EngineResult getFormulaResult(IndexParam indexParam, String whichData) {
		@SuppressWarnings("unchecked")
		Map<String, Object> whichDataFormulaInfos = (Map<String, Object>)indexParam.getParam(whichData);
		if(null == whichDataFormulaInfos) {
			return null;
		}
		EngineResult engineResult = (EngineResult)whichDataFormulaInfos.get(FormulaConsts.ITEM_FORMULA_RESULT);
		return engineResult;
	}
	
//	@SuppressWarnings("unchecked")
//	public static IndexParam addFormulaCells(IndexParam indexParam, String whichData, FormulaCells value) {
//		Map<String, Object> fvs = (Map<String, Object>)indexParam.getParam(whichData);
//		if(null == fvs) {
//			fvs = new HashMap<String, Object>();
//			indexParam.addParam(whichData, fvs);
//		}
//		fvs.put(FormulaConsts.ITEM_FORMULA_RESULT_CELLS, value);
//		return indexParam;
//	}
	
}
